<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");
global $dal; 

$village_id = getPostValue( 'village_id' );

$statement = $dal->prepare( 'SELECT DISTINCT buildings.building_id ' .
		'FROM village_buildings ' .
		'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
		'WHERE village_id = :village_id ' .
		'ORDER BY food_income DESC, ' .
			'coal_income DESC, ' .
			'iron_income DESC, ' .
			'housing DESC, ' .
			'space DESC, ' .
			'name' );
$statement->bindParam( ':village_id', $village_id, PDO::PARAM_INT );
$buildingsPresent = $dal->getArrayOfObjects( $statement );

$query = "SELECT ruler_id, name, loyalty, loyal_to " .
		"FROM village " .
		"WHERE village_id = '" . $village_id . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = $row[ 'name' ];
if( $row[ 'loyal_to' ] == $_SESSION[ 'ruler_id' ] && $row[ 'loyalty' ] > 50 ) {
	foreach( $buildingsPresent AS $buildingPresent ) {
		$remove = getPostValue( 'remove_' . $buildingPresent->building_id );
		if( isset( $remove ) && $remove == 'remove' ) {
			$building_id = $buildingPresent->building_id;
			$statement = $dal->prepare( 'SELECT space ' .
					'FROM buildings ' .
					'WHERE building_id = :building_id' );
			$statement->bindParam( ':building_id', $building_id, PDO::PARAM_INT );
			$space = $dal->getValue( $statement );
			if( $building_id != 0 && $space > 0 ) {
				$statement = $dal->prepare( 'SELECT building_id ' .
						'FROM buildings ' .
						'WHERE requires = :building_id' );
				$statement->bindParam( ':building_id', $building_id, PDO::PARAM_INT );
				$dependingOn = $dal->getArrayOfObjects( $statement );
				$foundOne = false;
				foreach( $dependingOn AS $building ) {
					foreach( $buildingsPresent AS $buildingPresent ) {
						if( $building->building_id == $buildingPresent->building_id ) {
							$foundOne = true;
							break 2;
						}
					}
				}
				
				if( !$foundOne ) {
					$amount = getPostValue( 'remove_amount_' . $building_id );
					if( is_numeric( $amount ) && $amount > 0 ) {
						if( $amount > 9 ) {
							$amount = 9;
						}
						$query = "SELECT count(*) " .
								"FROM village_queue " .
								"WHERE village_id = '" . $village_id . "'";
						$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
						$total = $row2[ 'count(*)' ];
						 
						while ( $total < getQueueLength() && $amount > 0 ) {
							$query = "INSERT INTO village_queue " .
									"( village_id, building_id, rank, destroy ) " .
									"VALUES ( '$village_id', '$building_id', $total, 1 )";
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							
							$query = "SELECT count(*) " .
									"FROM village_queue " .
									"WHERE village_id = '" . $village_id . "'";
							$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
							$total = $row2[ 'count(*)' ];
							
							$amount --;
						}

						if( $amount > 0 ) {
							$status = 'The village queue is full.';
							break 1;
						}
					}
					else {
						$status = 'Please select the number of buildings you want to be removed.';
					}
				}
				else {
					$status = "You have to remove all buildings requiring this building first.";
				}
			}
			else {
				$status = 'This building can\'t be removed.';
			}		 
		}
	}
}
else {
	$status = 'You can\'t remove any buildings here at the moment.';
}

if( $status != "" ) {
	$_SESSION[ 'que_building_status' ] = translate( $status );
}
if( isset( $name ) ) {
	header( "Location: village.php?name=$name" );
}
else {
	header( "Location: villages.php" );
}
?>